var path = require('path');
var PROTO_PATH = path.join(__dirname,'./protos/greeter.proto');

var grpc = require('@grpc/grpc-js');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {keepCase: true,
     longs: String,
     enums: String,
     defaults: true,
     oneofs: true
    });
var greeterProto = grpc.loadPackageDefinition(packageDefinition).greeter;

function sayHello(req, res) {
  console.log(req);   
  res(null, {message: 'success!',result:"Hello this is grpc server"});
}

var server = new grpc.Server();
server.addService(greeterProto.Greeter.service, {sayHello: sayHello});
server.bindAsync('0.0.0.0:3001', grpc.ServerCredentials.createInsecure(), () => {
  server.start();
  console.log('server started at 3001 ......');
});